<?php
//if (isset($_SESSION['username'])) {
include_once 'header.php';
require_once("conn.php");
require_once("change_page.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>乐知家园</title>
        <script language="javascript">            
            function search_key(){
                var key = document.getElementById("keyword").value;
                if(key==null||key==''){
                    location.href = "downloadStat.php";
                } else {
                    var page = document.getElementById("current_page").value;
                    location.href = "downloadStat.php?keyword=" + key + "&page=" + page+"&menu=download";
                }
            }
        </script>
        <style type="text/css">
            <!--
            body,td,th {
                font-family: 微软雅黑;
                font-size: 14px;
                color: #000000;
            }

            .STYLE1 {color: #FFFFFF}
            -->
        </style></head>
    <body>
        <br/>
        <table width="80%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF">
            <tr align="align">
                <td bgcolor="#FFFFFF">&nbsp;<input name="current_page" id="current_page" type="hidden" value="<?php echo $page; ?>" /></td>
                <td>搜索编号、名称 <input type="text" name="keywork" id="keyword" value="" /> <input type="button" value="搜索" onclick="search_key()" /></td>
                <td>&nbsp;<?php
$db = new DBC();
$sql = "SELECT SUM(downloadtimes) AS total FROM t_product WHERE downloadtimes > 0";
$total_down = $db->fetchData($sql);
if ($total_down != null) {
    echo "国内总下载数：<b>" . $total_down[0]['total'] . "</b>";
}
echo "&nbsp;&nbsp;";
$sql = "SELECT SUM(downloadtimes_en) AS total FROM t_product WHERE downloadtimes_en > 0";
$total_down = $db->fetchData($sql);
if ($total_down != null) {
    echo "国外总下载数：<b>" . $total_down[0]['total'] . "</b>";
}
?>
                </td>
                <td bgcolor="#FFFFFF" align="right"><?php
                    $page_size = 100;
                    if (isset($_GET['page'])) {
                        $page = $_GET['page'];
                    } else {
                        $page = 0;
                    }
                    if (isset($_GET['keyword'])) {
                        $keyword = $_GET['keyword'];
                        $keyword = trim($keyword);
                        $sql = "SELECT COUNT(*) AS total FROM t_product WHERE (flashcode LIKE '%" . $keyword . "%' OR name LIKE '%" . $keyword . "%') AND (downloadtimes > 0 || downloadtimes_en > 0)";
                    } else {
                        $sql = "SELECT COUNT(*) AS total FROM t_product WHERE downloadtimes > 0 || downloadtimes_en > 0";
                    }
                    $total_num = $db->fetchData($sql);
                    if ($total_num != null) {
                        $total_num_product = $total_num[0]['total'];
                    }
                    $page_count = getPageSize($total_num_product, $page_size);
                    echo getPage("fileList.php?menu=apk", $page_count, $page);
?></td>
            </tr>
        </table>
        <br/>
        <table width="80%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#9999FF">
            <tr align="center">
                <td bgcolor="#666666"><span class="STYLE1">序号</span></td>
                <td bgcolor="#666666"><span class="STYLE1">编号</span></td>
                <td bgcolor="#666666"><span class="STYLE1">名称</span></td>
                <td bgcolor="#666666"><span class="STYLE1"><a href="downloadStat.php?order=in&menu=download&keywork=<?php echo $_GET['keyword']; ?>">国内下载数</a></span></td>
                <td bgcolor="#666666"><span class="STYLE1"><a href="downloadStat.php?order=out&menu=download&keywork=<?php echo $_GET['keyword']; ?>">国外下载数</a></span></td>
            </tr>
            <?php
            $order = "downloadtimes";
            if (isset($_GET['order']) && $_GET['order'] == "out") {
                $order = "downloadtimes_en";
            }
            $begin = $page * $page_size;
            if (isset($_GET['keyword']) && $_GET['keyword'] != "") {
                $keyword = $_GET['keyword'];
                $keyword = trim($keyword);
                $sql = "SELECT flashcode,name,downloadtimes FROM t_product WHERE (downloadtimes > 0 || downloadtimes_en > 0) AND (flashcode LIKE '%" . $keyword . "%' OR name LIKE '%" . $keyword . "%') ORDER BY " . $order . " DESC LIMIT " . $begin . "," . $page_size;
            } else {
                $sql = "SELECT * FROM t_product WHERE (downloadtimes > 0 || downloadtimes_en > 0) ORDER BY " . $order . " DESC LIMIT " . $begin . "," . $page_size;
            }
            $num = $begin;
            $data = $db->fetchData($sql);
            foreach ($data as $product) {
                ?>
                <tr>
                    <td bgcolor="#FFFFFF">&nbsp;<?php echo++$num; ?></td>
                    <td bgcolor="#FFFFFF">&nbsp;<?php echo $product['flashcode']; ?></td>
                    <td bgcolor="#FFFFFF">&nbsp;<?php echo $product['name']; ?></td>
                    <td bgcolor="#FFFFFF">&nbsp;<?php echo $product['downloadtimes']; ?></td>
                    <td bgcolor="#FFFFFF">&nbsp;<?php echo $product['downloadtimes_en']; ?></td>
                </tr>
            <?php } ?>                    
        </table>
    </body>
</html>
<?php
include_once 'footer.php';
//} else {
//    header("login.php");
//}
?>